package com.yyy.instagram.Adaper;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.ContentFrameLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;



import com.yyy.instagram.Adaper.ViewHolder.GridTweetViewHolder;
import com.yyy.instagram.R;
import com.yyy.instagram.common.peekview.PeekViewActivity;
import com.yyy.instagram.common.peekview.builder.Peek;
import com.yyy.instagram.common.peekview.builder.PeekViewOptions;
import com.yyy.instagram.common.peekview.callback.SimpleOnPeek;

import static com.yyy.instagram.Adaper.UserTweetAdapter.DisplayType.grid;
import static com.yyy.instagram.Adaper.UserTweetAdapter.DisplayType.list;

/**
 * Created by qqqqqqq on 2016/10/10.
 */

public class UserTweetAdapter extends RecyclerViewAdapter {

    public enum DisplayType{
        list,grid
    }
    public final int HEADER = 0;
    public final int CONTENT = 1;
    public final int GRID_CONTENT = 2;

    public DisplayType type= grid;
    public Context context;

    public UserTweetAdapter(Context context){
        super();
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == HEADER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_header_user_tweet, parent, false);
            return new UserHeaderViewHolder(view);
        }
        if(viewType == GRID_CONTENT){
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_tweet_grid, parent, false);
            return new GridTweetViewHolder(view,context);
        }else {
            return super.onCreateViewHolder(parent, viewType);
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  UserHeaderViewHolder){
            UserHeaderViewHolder uhd = (UserHeaderViewHolder) holder;
            uhd.tab1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(type != grid){
                        type = grid;
                        UserTweetAdapter.this.notifyDataSetChanged();
                    }
                }
            });
            uhd.tab2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(type != list){
                        type = list;
                        UserTweetAdapter.this.notifyDataSetChanged();
                    }
                }
            });
        }
        if(holder instanceof GridTweetViewHolder){
            GridTweetViewHolder gholder = (GridTweetViewHolder) holder;
            initPopView(gholder.imageLeft);
            initPopView(gholder.imageCenter);
            initPopView(gholder.imageRight);


        }
        super.onBindViewHolder(holder,position);

    }

    public void initPopView(ImageView view){

        PeekViewOptions options = new PeekViewOptions();
//        options.setFullScreenPeek(true);
        options.setWidthPercent(0.9f);
        options.setHeightPercent(0.55f);
        options.setBackgroundDim(0.5f);
        options.setIsCenter(true);
        options.setBlurBackground(true);


        Peek.into(R.layout.pop_preview, new SimpleOnPeek() {
            @Override
            public void onInflated(View rootView) {

            }
        }).with(options).applyTo((PeekViewActivity) context,view);
    }



    @Override
    public long getHeaderId(int position) {
        if(type == grid){
            return -1;
        }
        if(position == 0){
            return -1;
        }
        return super.getHeaderId(position);
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tweet_header, parent, false);
        return new TweetHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return super.getItemCount()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return HEADER;
        }
        if(type == grid){
            return GRID_CONTENT;
        }
        return CONTENT;
    }

    public class UserHeaderViewHolder extends RecyclerView.ViewHolder{
        public ImageView tab1;
        public ImageView tab2;
        public ImageView tab3;
        public ImageView tab4;
        public UserHeaderViewHolder(View itemView) {
            super(itemView);
            tab1 = (ImageView) itemView.findViewById(R.id.item_header_user_tweet_tab1);
            tab2 = (ImageView) itemView.findViewById(R.id.item_header_user_tweet_tab2);
            tab3 = (ImageView) itemView.findViewById(R.id.item_header_user_tweet_tab3);
            tab4 = (ImageView) itemView.findViewById(R.id.item_header_user_tweet_tab4);
        }
    }


}
